package org.xhy.hrm.mq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import com.rabbitmq.client.Channel;
import org.springframework.stereotype.Component;
import org.xhy.hrm.dto.EmailSendDto;
import org.xhy.hrm.dto.SmsSendDto;
import org.xhy.hrm.dto.StationSendDto;


import java.io.IOException;

import static org.xhy.hrm.constants.MQConstants.*;

@Component
@Slf4j
public class MessageConsumer {

    //短信处理
    @RabbitListener(queues = NAME_QUEUE_HRM_SMS,containerFactory = "rabbitListenerContainerFactory")
    public void smsMessage(@Payload SmsSendDto smsSendDto, Message message, Channel channel) throws IOException {
        log.info("短信消费者的消息-{}",smsSendDto);
//        //发送短信
//        smsSendDto.getPhones().forEach(phone->{
//            log.info("发送的短信：phone={},content={}",phone,smsSendDto.getContent());
//        });
        //可保存到数据库

        //签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //短信处理
    @RabbitListener(queues = NAME_QUEUE_HRM_EMAIL,containerFactory = "rabbitListenerContainerFactory")
    public void emailMessage(@Payload EmailSendDto emailSendDto, Message message, Channel channel) throws IOException {
        log.info("邮件消费者的消息-{}",emailSendDto);
        //发送短信
        emailSendDto.getEmails().forEach(email->{
            log.info("发送的邮件：title={},email={},content={}",emailSendDto.getTitle(),email,emailSendDto.getContent());
        });
        //可保存到数据库

        //签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //短信处理
    @RabbitListener(queues = NAME_QUEUE_HRM_STATION_MSG,containerFactory = "rabbitListenerContainerFactory")
    public void stationMessage(@Payload StationSendDto stationSendDto, Message message, Channel channel) throws IOException {
        log.info("站内信消费者的消息-{}",stationSendDto);
        //发送短信
        stationSendDto.getUserIds().forEach(userIds->{
            log.info("发送的站内信：type={},userIds={},content={}",stationSendDto.getType(),userIds,stationSendDto.getContent());
        });
        //可保存到数据库

        //签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
